<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bericht extends MY_Controller {
    
    var $seizoen;

	function __construct(){
		parent::__construct();
		$this->load->library('berichtclass');
                $this->load->model('berichtmodel');
                
               
                
                
                
	}
	
	
	public function index()
	{
            $berichten = $this->berichtmodel->SelectAll();
            $data['berichten'] = $berichten;
            $this->LoadView('message_list', $data);
            
	}
        
        public function detail($id){
            $berichten = $this->berichtmodel->SelectById($id);
            $data['berichten'] = $berichten;
            $this->LoadView('message_detail', $data);
        }
        
        public function admin(){
            if($this->IsAdmin()){
                $unregistered = $this->ploegmodel->SelectNotInSeizoen($this->seizoen->Id);
		$ploegen = $this->ploegmodel->SelectBySeizoen($this->seizoen->Id);
                if(false !== $this->GetUpdate()){
                    $data['update'] = $this->GetUpdate();
                }
		//die(print_r($seizoenen));
		$data['ploegen'] = $ploegen;
                $data['unregistered'] = $unregistered;
		$this->LoadView('admin/ploeg', $data);
            } else { redirect(base_url()); }
            
        }
        
        
        public function create(){
            if($this->IsAdmin()){
                $this->LoadView('admin/bericht', null);
            }
            else {
                redirect(base_url());
            }
        }
	public function insertbericht()
	{
            if($this->IsAdmin()){
		//die(print_r($_POST));
		$titel = $this->input->post('titel');
                $bericht = $this->input->post('bericht');
                $type = $this->input->post('type');
                $stringdata = array($titel,$bericht,$type);
                 if(!$this->VerifyString($stringdata)){
                    $this->SetMessage("Invoer probleem. Actie geannulleerd.");
                    redirect(base_url() . 'bericht/create');
                }
                $this->berichtmodel->InsertBericht($titel,$bericht,$type);
                $this->SetMessage('Bericht toegevoegd');

		redirect(base_url());
	}
        else {
            redirect(base_url());
        }
        }
	
	public function deletebericht()
	{
            if($this->IsAdmin()){
		//$this->load->model('SeizoenModel');
		$id = $this->input->post('id');
                //die($id);
		$this->berichtmodel->DeleteBericht($id);
               
            }
		$this->GoBack();

	}
        
        public function insertcommentaar(){
            // die(print_r($this->input->post));
           $bericht = $this->input->post('bericht');
           $user = $this->input->post('user');
           $commentaar = $this->input->post('commentaar');
           $stringdata = array($commentaar);
           $numberdata = array($user,$bericht);
           if(!$this->VerifyNumber($numberdata) or !$this->VerifyString($stringdata)){
               $this->SetMessage('invoerfout');
               $this->GoBack();
           }
            if($this->IsAdmin()){
                $author = null;
            } else {
                $author = $user;
            }
            $this->berichtmodel->InsertCommentaar($bericht,$commentaar,$author);
            $this->GoBack('commentaar'.$id);
        }
        
	
	public function deletecommentaar()
	{
            $user = $this->GetUser();
            if($this->IsAdmin() or $this->input->post('user') == $user->Id){
                //die(print_r($_POST));
                $id = $this->input->post('id');
		if(!$this->VerifyNumber($id)){
                    $this->SetMessage('invoerfout');
                    $this->GoBack();
                }
                $this->berichtmodel->DeleteCommentaar($id);
                $this->SetMessage('Commentaar verwijderd');
            }
        
            $this->GoBack();
        }
	
}